package org.example.zrccddllljh.Controlelr;


import org.example.zrccddllljh.Pojo.Admin;
import org.example.zrccddllljh.Pojo.Result;
import org.example.zrccddllljh.Service.AdminService;
import org.example.zrccddllljh.utils.JwtUtil;
import org.example.zrccddllljh.utils.Md5Util;
import org.example.zrccddllljh.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/register")
    public Result register(String username, String password){


        Admin admin= adminService.findByUserName(username);
        if (admin==null){

            adminService.register(username,password);
            return Result.success();
        }else {

            return Result.error("用户名已经被占用");
        }
    }


    @PostMapping("/login")
    public Result<String> login( String username,  String password){

        Admin admin = adminService.findByUserName(username);

        if (admin == null) {
            return Result.error("用户名错误");
        }


        if (Md5Util.getMD5String(password).equals(admin.getPassword())) {

            Map<String, Object> claims = new HashMap<>();
            claims.put("id", admin.getId());
            claims.put("username", admin.getUsername());
            String token = JwtUtil.genToken(claims);

            return Result.success(token);
        }
        return Result.error("密码错误");

    }
    @GetMapping("/userInfo")
    public  Result<Admin> userinfo(){

        Map<String, Object> map=ThreadLocalUtil.get();
        String o =(String) map.get("username");
        Admin byUserName = adminService.findByUserName(o);
        return Result.success(byUserName);
    }


    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, String> params) {

        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {

            return Result.error("缺少必要的参数");
        }

        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Admin loginUser = adminService.findByUserName(username);
        if (!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("原密码填写不正确");
        }


        if (!rePwd.equals(newPwd)){
            return Result.error("两次填写的新密码不一样");
        }


        adminService.updatePwd(newPwd);
//        //删除对听的token
//        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
//        stringStringValueOperations.getOperations().delete(token);
        return Result.success();
    }

    @PatchMapping("updateAvatar")
    public Result updateAvatar(@RequestParam  String avatarUrl) {
        adminService.updateAvatar(avatarUrl);
        return Result.success();
    }


    @PutMapping("/update")
    public Result updara(@RequestBody @Validated Admin admin){
        adminService.updata(admin);
        return Result.success();
    }

    @GetMapping("/password")
    public List<Admin> password(){
        Map<String, Object> map=ThreadLocalUtil.get();
        String o =(String) map.get("username");
        List<Admin> a = adminService.password(o);
        return Result.success(a).getData();
    }

}
